//////////////////////////////////////////////////////////////////////////
// FileName:ucDivEmp
// CreateBy:Fenky
// CreateDate:2009-08-05
// Description:
//		       Show Emp info under division
//      
// Modify :20100127:Remove no use using --DragonXiong
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.Security;
//using BLL = Auto.Data.Systems.BLL;
//using Model = Auto.Data.Systems.Model;
using Auto.Web.Common;
using Auto.Data.HumanResource;
using Auto.Components;
namespace BaseSystem._System.UserControl
{

    public partial class ucDivEmp : System.Web.UI.UserControl
    {
        private bool _isEmpShowCheckBox = false;
        Division div = new Division();

        public delegate void EmpIDClickEventHandler(string empID);
        public event EmpIDClickEventHandler EmpID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        #region Tree Property

        public bool ShowCheckBox
        {
            set
            {
                _isEmpShowCheckBox = value;
            }
        }

        #region Get Value
        /// <summary>
        /// Gets or sets the checked nodes.
        /// </summary>
        /// <value>The checked nodes.</value>
        public IDictionary<string, string> CheckedNodes
        {
            get
            {
                IDictionary<string, string> result = new Dictionary<string, string>();
                foreach (TreeNode trChecked in trDivEmp.CheckedNodes)
                {
                    if (!result.ContainsKey(trChecked.Value))
                    {
                        result.Add(trChecked.Value, trChecked.Text);
                    }
                }
                return result;
            }
        }
        #endregion Get Value

        #endregion Tree Property

        #region Tree Init


        /// <summary>
        /// Trees the init.
        /// </summary>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="sysName">Name of the sys.</param>
        public void TreeInit()
        {
            this.trDivEmp.Nodes.Clear();

            DataTable topDivs = div.GetList("ParentID=''");

            LoadRootNode(topDivs);
        }

        /// <summary>
        /// Loads the root node.
        /// </summary>
        /// <param name="topFuncs">The top funcs.</param>
        private void LoadRootNode(DataTable topDivs/*XmlNode xmlNode*/)
        {

            TreeNode rootNode;

            foreach (DataRow dr in topDivs.Rows)
            {
                rootNode = new TreeNode();
                rootNode.Text = dr[Division.FIELD_DivName].ToString();
                rootNode.Value = dr[Division.FIELD_DivID].ToString();

                rootNode.SelectAction = TreeNodeSelectAction.Expand;
                rootNode.ImageUrl = "../css/Images/folder.gif";
                this.trDivEmp.Nodes.Add(rootNode);
                DataTable dt = div.GetDataByKey("", rootNode.Value);

                LoadEmpBasicTreeNodes(rootNode, rootNode.Value);

                LoadChildTreeNodes(rootNode, dt);
            }
        }
        /// <summary>
        /// Loads the fun node.
        /// </summary>
        /// <param name="treeNode">The tree node.</param>
        /// <param name="dtsubFuncs">The dtsub funcs.</param>
        private void LoadChildTreeNodes(TreeNode treeNode, DataTable dtsubFuncs/* XmlNode xmlNode*/)
        {
            TreeNode node;

            foreach (DataRow dr in dtsubFuncs.Rows/*XmlElement xe in xmlNode.ChildNodes*/)
            {
                node = new TreeNode();
                node.Text = dr[Division.FIELD_DivName].ToString();
                node.Value = dr[Division.FIELD_DivID].ToString();


                TreeNode patNode = treeNode;

                node.SelectAction = TreeNodeSelectAction.Expand;
                node.ImageUrl = "../css/Images/folder.gif";
                treeNode.ChildNodes.Add(node);
                LoadEmpBasicTreeNodes(node, node.Value);
                DataTable dt = div.GetDataByKey("", node.Value);
                LoadChildTreeNodes(node, dt);
            }
        }
        /// <summary>
        /// Loads the fun node.
        /// </summary>
        /// <param name="treeNode">The tree node.</param>
        /// <param name="dtsubFuncs">The dtsub funcs.</param>
        private void LoadEmpBasicTreeNodes(TreeNode treeNode, string divID/* XmlNode xmlNode*/)
        {
            TreeNode node;

            EmpDivisions empDiv = new EmpDivisions();

            
            DataTable dtEmp = empDiv.GetEmpBasicByDivID(divID);
            foreach (DataRow dr in dtEmp.Rows/*XmlElement xe in xmlNode.ChildNodes*/)
            {
                node = new TreeNode();
                node.Text = dr[EmpBasic.FIELD_Name].ToString();
                node.Value = dr[EmpBasic.FIELD_EmpID].ToString();

                node.SelectAction = TreeNodeSelectAction.SelectExpand;
                if (_isEmpShowCheckBox)
                {
                    node.ShowCheckBox = _isEmpShowCheckBox;
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }
                if (StrHelper.GetBoolValue(dr[EmpBasic.FIELD_sex]))
                {
                    node.ImageUrl = "../css/Images/U01.gif";
                }
                else
                {
                    node.ImageUrl = "../css/Images/U11.gif";
                }
                treeNode.ChildNodes.Add(node);

                //DataTable dt = div.GetDataByKey("", node.Value);
                //LoadChildTreeNodes(node, dt);
            }
        }

        /// <summary>
        /// Loads the operation list.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="funcID">The func ID.</param>
        private void LoadOperationList(TreeNode node, string sysID, string funcID)
        {
            SysFuncOperate operate = new SysFuncOperate();
            DataTable dt = operate.GetRoleFunc(sysID, funcID, "");
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode childNode = new TreeNode();
                childNode.Value = dr[SysRoleFuncOperate.FIELD_OperateID].ToString();
                childNode.Text = dr[OperateList.FIELD_OperateName].ToString();
                childNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(childNode);

            }
        }

        /// <summary>
        /// Loads the role func.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="funcID">The func ID.</param>
        private void LoadRoleFunc(TreeNode node, string sysID, string funcID)
        {
            //BLL.SysFunctions subFuncs = new BLL.SysFunctions();
            //DataTable subSysFuncs = subFuncs.GetSysFunctionsByParentID(SessionBox.CultrueInfo, sysID.ToString(), funcID);

            //LoadFunNode(node, subSysFuncs);
        }
        #endregion Tree Init

        protected void trDivEmp_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (EmpID != null)
            {
                EmpID(this.trDivEmp.SelectedNode.Value);
                this.trDivEmp.SelectedNode.Selected = false;
            }
        }
    }
}